<!--
 * @author wangpeizhi
 -->
<template>
  <div class="lottery-manage">
    <div class="lottery-manage-title">新建/编辑抽奖活动</div>
    <section v-loading="loading" class="lottery-manage-main">
      <el-form ref="form" :model="form" :rules="rules" label-width="240px" label-position="center" class="form">
        <el-form-item label="抽奖名称" prop="bigturntableName">
          <el-input v-model.trim="form.bigturntableName" />
        </el-form-item>
        <el-form-item label="开始日期" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="选择开始日期"
            style="width: 100%;"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="结束日期" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="选择结束日期"
            style="width: 100%;"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="抽奖类型" prop="drawForm">
          <el-radio-group v-model="form.drawForm">
            <el-radio label="CJXS_DZP">大转盘</el-radio>
            <el-radio label="CJXS_LHJ">老虎机</el-radio>
            <el-radio label="CJXS_JGG">九宫格</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="单个用户每日抽奖次数限制" prop="drawEverydayLimit">
          <el-radio-group v-model="form.drawEverydayLimit">
            <el-radio :label="0">不限制</el-radio>
            <el-radio :label="1">限制</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.drawEverydayLimit == 1" label="单个用户每日抽奖次数上限" prop="drawEverydayToplimit">
          <el-input-number v-model="form.drawEverydayToplimit" />
        </el-form-item>
        <el-form-item label="单个用户每日中奖次数限制" prop="winEverydayLimit">
          <el-radio-group v-model="form.winEverydayLimit">
            <el-radio :label="0">不限制</el-radio>
            <el-radio :label="1">限制</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.winEverydayLimit == 1" label="单个用户每日中奖次数上限" prop="winEverydayToplimit">
          <el-input-number v-model="form.winEverydayToplimit" />
        </el-form-item>
        <el-form-item label="单个用户在整个活动中奖次数限制" prop="winActivityLimit">
          <el-radio-group v-model="form.winActivityLimit">
            <el-radio :label="0">不限制</el-radio>
            <el-radio :label="1">限制</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.winActivityLimit == 1" label="单个用户整个活动中奖次数上限" prop="winActivityToplimit">
          <el-input-number v-model="form.winActivityToplimit" />
        </el-form-item>
        <el-form-item label="活动每日中奖次数限制" prop="winActivityEverydayLimit">
          <el-radio-group v-model="form.winActivityEverydayLimit">
            <el-radio :label="0">不限制</el-radio>
            <el-radio :label="1">限制</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.winActivityEverydayLimit == 1" label="活动每日中奖次数上限" prop="winActivityEverydayToplimit">
          <el-input-number v-model="form.winActivityEverydayToplimit" />
        </el-form-item>
        <el-form-item label="抽奖说明" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" maxlength="500" placeholder="请输入抽奖说明" />
        </el-form-item>
      </el-form>
    </section>

    <section class="lottery-manage-footer">
      <el-button style="padding: 10px 30px;" @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleComit('form')">确 定</el-button>
    </section>
  </div>
</template>
<script>
import { specialText } from '@/utils/validate'
import { getDictByCode, insert, update, get } from './../api/activity'
export default {
  name: 'LotteryManage',
  data() {
    var checkEnd = (rule, value, callback) => {
      if (this.form.startTime === '') {
        callback(new Error('请选择开始日期'))
      } else {
        if (new Date(this.form.startTime).getTime() > new Date(value).getTime()) {
          callback(new Error('结束日期不能早于开始日期'))
        }
        callback()
      }
    }
    return {
      loading: false,
      form: {
        id: '',
        bigturntableName: '',
        remarks: '',
        startTime: '',
        endTime: '',
        prizeCount: '',
        gaveNumber: 0,
        drawForm: 'CJXS_DZP',
        status: 'BIGTSTATUS02',
        drawEverydayLimit: 0,
        drawEverydayToplimit: 0,
        winEverydayLimit: 0,
        winEverydayToplimit: 0,
        winActivityLimit: 0,
        winActivityToplimit: 0,
        winActivityEverydayLimit: 0,
        winActivityEverydayToplimit: 0
      },
      rules: {
        bigturntableName: [
          {
            required: true,
            message: '请输入抽奖名称',
            trigger: 'blur'
          },
          {
            validator: specialText,
            trigger: 'blur'
          },
          {
            min: 1,
            max: 50,
            message: '长度在 1 到 50 个字符',
            trigger: 'blur'
          }
        ],
        startTime: [
          {
            required: true,
            message: '请选择开始日期',
            trigger: 'change'
          }
        ],
        endTime: [
          {
            required: true,
            message: '请选择结束日期',
            trigger: 'change'
          },
          {
            required: true,
            validator: checkEnd,
            trigger: 'change'
          }
        ],
        drawForm: [
          {
            required: true,
            message: '请选择抽奖类型',
            trigger: 'change'
          }
        ],
        remarks: [
          {
            required: true,
            message: '请输入抽奖说明',
            trigger: 'blur'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],
        drawEverydayLimit: [
          {
            required: true,
            message: '请选择是否限制用户每日抽奖次数',
            trigger: 'change'
          }
        ],
        drawEverydayToplimit: [
          {
            required: true,
            message: '请设置用户每日抽奖次数上限',
            trigger: 'blur'
          }
        ],
        winEverydayLimit: [
          {
            required: true,
            message: '请选择是否限制用户每日中奖次数',
            trigger: 'change'
          }
        ],
        winEverydayToplimit: [
          {
            required: true,
            message: '请设置用户每日中奖次数上限',
            trigger: 'blur'
          }
        ],
        winActivityLimit: [
          {
            required: true,
            message: '请选择是否限制用户在整个活动中奖次数',
            trigger: 'change'
          }
        ],
        winActivityToplimit: [
          {
            required: true,
            message: '请设置用户在整个活动中奖次数上限',
            trigger: 'blur'
          }
        ],
        winActivityEverydayLimit: [
          {
            required: true,
            message: '请选择是否限制活动每日中奖次数',
            trigger: 'change'
          }
        ],
        winActivityEverydayToplimit: [
          {
            required: true,
            message: '请设置活动每日中奖次数上限',
            trigger: 'blur'
          }
        ]
      },
      state: 'create',
      t: '0',
      dict: {
        BIGTSTATUS: [] // 状态
      }
    }
  },
  created() {
    this.state = this.$route.query.type === 'c' ? 'create' : 'update'
    for (const key in this.dict) {
      this.getDict(key)
    }
    if (this.$route.query.type === 'u') {
      if (this.$route.query.i) {
        this.getData(this.$route.query.i)
      } else {
        this.$message({
          showClose: true,
          message: '参数异常！',
          type: 'warning',
          onClose: () => {
            this.$router.back()
          }
        })
      }
    }
  },
  methods: {
    /**
     * 获取字典
     */
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code)
          .then((res) => {
            this.dict[code] = res.data.data && res.data.data.length > 0 ? res.data.data[0].list : []
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    /**
     * 根编辑回显
     */
    getData(id) {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        get(id)
          .then((res) => {
            loading.close()
            const data = res.data.data
            for (const key in this.form) {
              this.form[key] = data[key]
            }
            this.showClassify = true
          })
          .catch((error) => {
            loading.close()
            reject(error)
          })
      })
    },

    /**
     * 取消
     */
    handleCancel() {
      this.$router.push({
        path: this.$filterPath('index'),
        query: {
          t: this.t
        }
      })
    },

    handleComit() {
      if (this.state === 'create') {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            const loading = this.$loading({
              lock: true,
              text: '请稍后',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.3)'
            })
            const params = Object.assign({}, this.form)
            return new Promise((resolve, reject) => {
              insert(params)
                .then(() => {
                  this.dialogVisible = false
                  loading.close()
                  this.$message({
                    title: '成功',
                    message: '创建成功',
                    type: 'success',
                    duration: 2000
                  })
                  this.$router.push({
                    path: this.$filterPath('index')
                  })
                })
                .catch((error) => {
                  reject(error)
                  loading.close()
                })
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      } else {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            const loading = this.$loading({
              lock: true,
              text: '请稍后',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.3)'
            })
            const params = Object.assign({}, this.form)
            return new Promise((resolve, reject) => {
              update(params)
                .then(() => {
                  this.dialogVisible = false
                  loading.close()
                  this.$message({
                    title: '成功',
                    message: '修改成功',
                    type: 'success',
                    duration: 2000
                  })
                  this.$router.push({
                    path: this.$filterPath('index')
                  })
                })
                .catch((error) => {
                  reject(error)
                  loading.close()
                })
            })
          } else {
            return false
          }
        })
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
@import '@/styles/mixin.scss';

.lottery-manage {
  background-color: #fff;
  min-height: calc(100vh - 60px);
  max-height: calc(100vh - 60px);
  overflow: hidden;

  .lottery-manage-title {
    position: relative;
    height: 40px;
    line-height: 40px;
    padding: 0 20px;
    background-color: #ffffff;
    border-bottom: 1px solid #e5e5e5;
    font-family: PingFangSC-Medium;
    font-weight: bold;
    color: rgba(51, 51, 51, 1);
    font-size: 14px;

    &:before {
      content: '';
      position: absolute;
      width: 2px;
      @include bg-color($mainColor);
      top: 13px;
      bottom: 13px;
      left: 15px;
    }
  }

  .lottery-manage-main {
    position: absolute;
    top: 50px;
    left: 0;
    right: 0;
    bottom: 52px;
    box-sizing: border-box;
    padding: 0 10px;
    overflow: hidden;

    &::-webkit-scrollbar {
      width: 8px;
      /*对垂直流动条有效*/
      height: 1px;
      /*对水平流动条有效*/
    }

    /** 滚动条轨道*/
    &::-webkit-scrollbar-track {
      box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
      background-color: white;
      border-radius: 3px;
    }

    /** 滚动条里面小方块*/
    &::-webkit-scrollbar-thumb {
      height: 32px;
      border-radius: 5px;
      box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.1);
      background-color: #eaeaea;
    }

    &:hover {
      overflow: auto;
    }

    .form {
      width: 1000px !important;
      margin: 0 auto;

      .el-select {
        width: 100%;
      }

      .line {
        text-align: center;
      }
    }

    .select-data {
      display: inline-block;
      background: rgba(246, 246, 246, 1);
      border-radius: 4px;
      font-size: 14px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      color: rgba(51, 51, 51, 1);
      padding: 0 10px;
      margin-right: 5px;
      margin-bottom: 8px;

      i {
        cursor: pointer;
        padding-left: 5px;
      }
    }

    .selectbtn {
      display: inline-block;
      width: 120px;
      background: rgba(255, 255, 255, 1);
      @include border-color(1px, dotted, $mainColor);
      border-radius: 4px;
      font-size: 14px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      @include font-color($mainColor);
      text-align: center;
      cursor: pointer;
    }
  }

  .lottery-manage-footer {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    text-align: center;
    padding: 10px 0;
    background-color: white;
    box-shadow: 0 -3px 6px #f5f5f5;
  }
}
</style>
<style lang="scss">
@import '@/styles/mixin.scss';

.lottery-manage {
  .classify-tree-con {
    position: relative;
    height: 400px;
    overflow: hidden;

    .classify-tree-con-scrollbar {
      overflow: hidden;

      .el-scrollbar__wrap {
        max-height: 381px;
      }
    }

    .custom-tree-node {
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;
      width: 100%;

      .tree-node-txt {
        flex: 1;
      }
    }
  }
}
</style>
